#encoding=utf-8

#https://tieba.baidu.com/f?kw=%E8%BF%90%E7%AD%B9%E5%AD%A6&ie=utf-8&pn=50

import urllib 
import urllib2
import random


USER_AGENT_LIST = [
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
                "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6"
                    ]

def send_request():
    base_url = "https://tieba.baidu.com/f?"
    
    str_dict = {'kw':raw_input("请输入要查询的贴吧名:"),'pn':int(raw_input("请输入查询贴吧第几页内容:"))*50}

    request_str = urllib.urlencode(str_dict)

    full_url = base_url + request_str

    user_agent = random.choice(USER_AGENT_LIST)
    
    request = urllib2.Request(full_url)
    
    request.add_header("User-Agent", user_agent)

    response = urllib2.urlopen(request)

    return response.read()


if __name__ == "__main__":
    html = send_request()
    with open('task.html','w') as f:
        f.write(html)

